<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class ModifyUserClass extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $users = \App\Model\User::where('class', '!=', null)->get(['id', 'class', 'college']);
        Schema::table('user', function ($table) {
            $table->integer('class_id')->nullable()->after('role');
            $table->dropColumn('class');
            $table->dropColumn('college');
        });
        foreach ($users as $user) {
            if($user->class){
                $class = \App\Model\Classes::firstOrCreate(['name' => $user->class, 'college' => $user->college]);
                \App\Model\User::find($user->id)->update(['class_id' => $class->id]);
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}
